package my.diplom.web.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.security.context.SecurityContext;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.security.Authentication;
import org.springframework.security.GrantedAuthority;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import my.diplom.util.UserUtil;
import my.diplom.util.JsonUtil;
import my.diplom.util.HostUtil;
import my.diplom.dao.service.ResourceService;
import my.diplom.dao.service.HostService;
import my.diplom.entity.Resource;
import my.diplom.entity.Host;

import java.util.Date;
import java.util.List;
import java.util.HashMap;
import java.util.ArrayList;

import com.google.gson.Gson;

/**
 * Created by IntelliJ IDEA.
 * User: gb
 * Date: 06.08.2009
 * Time: 0:42:26
 * To change this template use File | Settings | File Templates.
 */

public class HomeController extends AbstractController {

    private ResourceService resourceService;
    private HostService hostService;

    public void setHostService(HostService hostService) {
        this.hostService = hostService;
    }

    public void setResourceService(ResourceService resourceService) {
        this.resourceService = resourceService;
    }

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.out.println("Date = " + new Date());
        HashMap<String, Object> model = new HashMap<String, Object>();
        HashMap<Host, String> hostsMap = new HashMap<Host, String>();
        getServletContext().getInitParameter("paramName");
        if (UserUtil.isAdmin()) {
           // List<Host> hosts = hostService.getAll();
            List<Host> hosts = hostService.getActiveHosts();
            HostUtil.setUsage(hosts);
            for (Host h : hosts) {
                String json = JsonUtil.resourcesToJson(new ArrayList(h.getResources()));
                System.out.println(json);
                hostsMap.put(h, json);
            }

//            List<Resource> resources = resourceService.getAll(true);
//            String json = JsonUtil.resourcesToJson(resources);
//            System.out.println(json);
//
//            model.put("json", json);

            model.put("hostMap", hostsMap);
            return new ModelAndView("admin", model);
        } else {

            List<Resource> resources = resourceService.getResourcesByLogin(UserUtil.getRemoteUser());
            String json =  JsonUtil.resourcesToJson(resources);
            model.put("json", json);
            return new ModelAndView("user", model);

        }

    }

}
